package cn.itcast.health.provider.service.impl;

import cn.itcast.health.commons.entity.Result;
import cn.itcast.health.commons.resources.MessageConstant;
import cn.itcast.health.commons.utils.DateUtils;
import cn.itcast.health.interfaces.service.OrderService;
import cn.itcast.health.mapper.OrderMapper;
import cn.itcast.health.pojo.Member;
import cn.itcast.health.pojo.Order;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public Result addOrderData(Map map) {
        Result result = null;
        try {
            //获取参数
            String name = (String) map.get("name"); //姓名
            String sex = (String) map.get("sex"); //性别
            String telephone = (String) map.get("telephone"); //手机号
            String idCard = (String) map.get("idCard"); //身份证号
            String orderDate = (String) map.get("orderDate"); //日期
            String orderType = (String) map.get("orderType"); //预约类型
            String setmealId = (String) map.get("setmealId"); //套餐id

            //根据体检日期 判断是否可以预约
            int row = orderMapper.isOrdersetting(1,orderDate);


            //创建对象
            Member member = new Member();
            member.setIdCard(idCard);
            member.setName(name);
            member.setPhoneNumber(telephone);
            member.setSex(sex);
            member.setRegTime(new Date());
            member.setBirthday(new Date());

            //创建会员或跟新会员 根据手机
            orderMapper.isMemberOrUpdate(member);
            Member member1 = orderMapper.findByPhoneMember(member.getPhoneNumber());

            //创建map
            Map map1 = new HashMap();
            map1.put("member",member1);

            //判断是否可以预约
            if (row > 0){
                //创建预约信息对象
                Order order = new Order();
                order.setSetmealId(Integer.parseInt(setmealId));
                order.setOrderDate(DateUtils.parseString2Date(orderDate,"yyyy-MM-dd"));
                order.setOrderStatus(Order.ORDERSTATUS_NO);
                order.setOrderType(orderType);
                order.setMemberId(member1.getId());
                //判断是否预约过
                Order orde = orderMapper.findByIdOrder(member1.getId(),orderDate, setmealId);
                if(orde != null){
                    // 已预约过
                    orderMapper.isOrdersetting(-1,orderDate);
                    map1.put("order",orde);
                    result = new Result(true, MessageConstant.HAS_ORDERED,map1);
                }else {
                    //预约成功
                    orderMapper.addOrderData(order);
                    orde = orderMapper.findByIdOrder(member1.getId(),orderDate, setmealId);
                    map1.put("order",orde);
                    result = new Result(true, MessageConstant.ADD_ORDER_SUCCESS,map1);
                }
            }else {
                result = new Result(true, MessageConstant.ORDER_FULL,map1);
            }
        }catch (Exception e){
            result = new Result(false, MessageConstant.ADD_ORDER_FAIL);
        }
        return result;
    }
}
